package com.masterclient.app.businessmodule.homemodule.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.old.EventAdDataCallback;
import com.masterclient.app.businessmodule.homemodule.view.holder.HomeViewHolder;
import com.masterclient.app.modules.income.model.HomeDataBean;
import com.masterclient.app.utils.CommonUtil;

import java.util.List;


public class AdGroupAdapter extends DelegateAdapter.Adapter<HomeViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;

    private List<HomeDataBean.HomeAdsItem> adGroupDomains;

    private EventAdDataCallback eventAdDataCallback;
    boolean hasJg = false;

    public void setEventAdDataCallback(EventAdDataCallback eventAdDataCallback) {
        this.eventAdDataCallback = eventAdDataCallback;
    }

    public AdGroupAdapter(Context context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null, null,true);
    }

    public AdGroupAdapter(Context context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams,
                          List<HomeDataBean.HomeAdsItem> adGroupDomains,boolean hasJg) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.adGroupDomains = adGroupDomains;
        this.hasJg = hasJg;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    protected void onBindViewHolderWithOffset(HomeViewHolder holder, int position, int offsetTotal) {
    }

    @Override
    public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(HomeViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        if (adGroupDomains.size() == 3) {
            ImageView ivLeftPic = holder.itemView.findViewById(R.id.iv_lef_pic);
            ImageView ivTopPic = holder.itemView.findViewById(R.id.iv_top_pic);
            ImageView ivBottomPic = holder.itemView.findViewById(R.id.iv_bottom_pic);
            ivLeftPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.get(0).targetUrl,""
                            ,"","","","");
                }
            });
            ivTopPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.get(1).targetUrl,""
                            ,"","","","");
                }
            });
            ivBottomPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.goAnyWhere(mContext,adGroupDomains.get(2).targetUrl,""
                            ,"","","","");
                }
            });
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.get(0).banner, ivLeftPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.get(1).banner, ivTopPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
            ViewTransformUtil.glideImageView(mContext, adGroupDomains.get(2).banner, ivBottomPic,new Transformation[]{new CenterCrop()},R.mipmap.defaults_1);
        }
    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }
}
